package cn.leetcode;

public class Code19 {

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null){
            return null;
        }
        int num = 0;
        ListNode node = head;
        ListNode flag = head;
        while (node != null){
            num++;
            node = node.next;
        }
        int i = num - n;
        if(i == 0){
           head = head.next;
           return head;
        }
        for(int j=0; j<num; j++){
            if(j==i-1){
                flag.next = flag.next.next;
                break;
            }
            flag = flag.next;
        }
        return head;
    }
}
